<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\News\Atla;
use App\Models\News\AtlasList;
use App\Models\News\News;
use App\Models\News\NewsClass;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Models\Category;
use Encore\Admin\Widgets\Table;
class AtlaController extends Controller
{
    protected $name = '图集管理';
    use ModelForm;
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
	//删除图集其中一个图片
    public function delete_atlas_item()
	{
		$id = request()->get('id', 0);
		if($id >0)
		{
			AtlasList::where('id', $id)->delete();
			return ['msg'=> '删除成功.'];
		}else{
			return ['msg'=> '删除失败.'];
		}
	}
	//搜索图集
	public function search()
	{
        $keyword = request()->get('keyword', '');
		$q = request()->get('q', '');
		if($keyword != '')
		{
			$list = Atla::where('title', 'like', '%'.$keyword.'%')->limit(10)->get();
			if($list->isEmpty())
			{
				return ['status'=>0,'html'=> '没有了.'];
			}
			$headers = ['Id', '标题', '操作'];
			$rows = [];
			foreach($list as $k=>$row)
			{
				$url = 'javascript:;';
				$thumb = '';
				if($row->lists)
				{
					$thumb = $row->lists->first()->thumb;
				}
				$rows[] = [
					$row->id, $row->title, '<a class="btn btn-info btn-sm" href="'.$url.'" data-src="'.$thumb.'" data-id="'.$row->id.'"><i class="fa fa-mouse-pointer"></i>&nbsp;选择</a>'
				];
			}
			$table = new Table($headers, $rows);
			return ['html' => $table->render()];
		}elseif($q != ''){
            return Atla::where('title', 'like', "%$q%")->paginate(null, ['id', 'title as text']);
        }else{
			return ['html'=>'关键词不能为空.'];
		}
	}
	
	
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Atla::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            
            $grid->filter(function($filter){
                
                $filter->like('title', '标题');
                //$filter->addFilter($filter->like('post_title', '标题'));
                
            
            });
            $grid->column('id', 'ID')->sortable();
            $grid->column('listorder', '排序')->editable();
            
            $grid->column('title', '名称');
			$grid->column('news_class.name', '栏目');
            $grid->column('admin_user.name', '发布');
            $grid->column('admin_edit_user.name', '最后更新');
            
            $grid->column('updated_at', '更新时间');
        });
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        
        return Admin::form(Atla::class, function (Form $form) {
            
            $form->number('id', 'ID')->attribute('disabled', false);
            $form->text('title', '标题')->rules('required|min:1');
            $form->text('keywords', '关键词');
            $form->text('description', '描述');
            $form->text('source', '来源');
            $form->text('author', '作者');
			$form->select('class_id', '栏目')->options(NewsClass::selectOptions())->rules('required');
            $form->radio('status','审核状态')->options(News::$status)->default('1');
			$form->atla('lists', '图集上传');
			$form->saved(function (Form $form) {
				$data = [];
				foreach($form->files as $k=>$row)
				{
					$tmp = [
						'atlas_id' => $form->model()->id,
						'thumb' => $row,
						'listorder' => $k,
					];
					$tmp['title'] = '';
					if(array_key_exists($k, $form->smalltext))
					{
						$tmp['title'] = $form->smalltext[$k];
					}
                    if(array_key_exists($k, $form->listorder))
                    {
                        $tmp['listorder'] = $form->listorder[$k];
                    }
					if($form->list_id)
					{
						if(array_key_exists($k, $form->list_id))
						{
							$AtlasList = AtlasList::find($form->list_id[$k]);
                            $AtlasList->title = $tmp['title'];
							$AtlasList->listorder = $tmp['listorder'];
							$AtlasList->save();
							continue;
						}
					}
					$data[] = $tmp;
				}
                // dd($data);
				if(!empty($data))AtlasList::insert($data);

			});		
            
        });
    }
}
